package CustomClassFile.yueli;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;

import CustomClassFile.CronJob.DateUtil;
import CustomClassFile.CronJob.Pbzxx;
import weaver.conn.RecordSet;
import weaver.general.StaticObj;

public class Yueli {
	public static final Log logger = LogFactory.getLog(Yueli.class);

	// 统计月历天数
	public static Map<String, Integer> getcalendarDays(String date) {
		// 截取日期为年月 yyyy-MM
		date = StringUtils.trim(date).substring(0, 7);

		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("xtype1", 0);
		map.put("xtype2", 0);
		map.put("xtype3", 0);

		java.sql.Connection conn = null;
		try {
			weaver.interfaces.datasource.DataSource ds = (weaver.interfaces.datasource.DataSource) StaticObj
					.getServiceByFullname(("datasource.HRDB"), weaver.interfaces.datasource.DataSource.class);
			conn = ds.getConnection();
			String sql = "select xType,count(*) as days from zswsHRaCalendar where  convert(VARCHAR(7),term,20)=? group by xType;";
			PreparedStatement preparedStatement = conn.prepareStatement(sql);
			preparedStatement.setString(1, date);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				String xtype = rs.getString("xType");
				int days = rs.getInt("days");
				String key = "xtype" + xtype;
				map.put(key, days);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	// 统计排班的天数
	public static Map<String, Integer> getPaiBanDays(String id, String date) {
		int Monthdays=getDayOfMonth(date);//得到日期月份的天数
		date = StringUtils.trim(date).substring(0, 7);//得到年月  yyyy-MM
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("xtype1", 0);
		map.put("xtype2", 0);
		map.put("xtype3", 0);

		RecordSet rs = new RecordSet();
		StringBuffer sb = new StringBuffer();
		sb.append(
				"select z.xtype,count(*) as days from hrmresource  h inner join uf_rypbxx z on h.outkey=z.eid and h.id=? and convert(VARCHAR(7),z.term,20)=? group by z.xtype");
		rs.executeQuery(sb.toString(), id, date);
		while (rs.next()) {
			String xtype = rs.getString("xType");
			int days = rs.getInt("days");
			String key = "xtype" + xtype;
			map.put(key, days);
		}
		
		RecordSet rs1 = new RecordSet();
		String sql_xtype2="select count(z.id) as days from hrmresource  h inner join uf_rypbxx z on h.outkey=z.eid and h.id=? and convert(VARCHAR(7),z.term,20)=? and z.shift='R'";
		rs1.executeQuery(sql_xtype2, id,date);
		if(rs1.next()) {
			map.put("xtype2", rs1.getInt("days"));//休息天数(含公休和节假日)
			if (map.get("xtype1")!=0) {
				map.put("xtype1", Monthdays-map.get("xtype2"));//工作天数
			}
			
		}
		
		map.put("xtype2_3",  map.get("xtype2"));
		return map;
	}
	
	/**
	 *  java获取当前月的天数
	 * @param date yyyy-MM-dd
	 * @return
	 */
	public static int getDayOfMonth(String date){
		Date ddate=DateUtil.StringToDate(date, "yyyy-MM-dd");
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(ddate);
		int day=aCalendar.getActualMaximum(Calendar.DATE);
		return day;
	}

	// 统计排班的天数
	public static Map<String, Integer> getPaiBanDays_old(String id, String date) {
		date = StringUtils.trim(date).substring(0, 7);

		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("xtype1", 0);
		map.put("xtype2", 0);
		map.put("xtype3", 0);

		RecordSet rs = new RecordSet();
		StringBuffer sb = new StringBuffer();
		sb.append(
				"select z.xtype,count(*) as days from hrmresource  h inner join uf_rypbxx z on h.outkey=z.eid and h.id=? and convert(VARCHAR(7),z.term,20)=? group by z.xtype");
		rs.executeQuery(sb.toString(), id, date);
		while (rs.next()) {
			String xtype = rs.getString("xType");
			int days = rs.getInt("days");
			String key = "xtype" + xtype;
			map.put(key, days);

		}
		String key = "xtype2_3";
		int value = map.get("xtype2") + map.get("xtype3");
		map.put(key, value);
		return map;
	}
	
	// 查询排班组信息
	public static Pbzxx getPbzxx(String id, String date) {
		String sql = "select t.* from hrmresource  h inner join uf_rypbxx z on h.outkey=z.eid and h.id=? and CONVERT(date,z.term,20)=? inner join uf_pbzxx t "
				+ "on z.shift=t.shift and (t.isdisabled='0' or t.isdisabled is null)";
		RecordSet rs = new RecordSet();
		rs.executeQuery(sql, id, date);
		Pbzxx pbz = null;
		while (rs.next()) {
			pbz = new Pbzxx();
			pbz.setShift(rs.getString("shift"));
			pbz.setTitle(rs.getString("title"));
			pbz.setCard1(rs.getString("card1"));
			pbz.setCard2(rs.getString("card2"));
			pbz.setCard3(rs.getString("card3"));
			pbz.setCard4(rs.getString("card4"));
			pbz.setScan1(rs.getString("scan1"));
			pbz.setScan2(rs.getString("scan2"));
			pbz.setScan3(rs.getString("scan3"));
			pbz.setScan4(rs.getString("scan4"));
			pbz.setEndT1(rs.getString("endT1"));
			pbz.setEndT2(rs.getString("endT2"));
			pbz.setEndT3(rs.getString("endT3"));
			pbz.setEndT4(rs.getString("endT4"));
			pbz.setXhours(rs.getString("Xhours"));
			pbz.setCardTimes(rs.getString("CardTimes"));
		}
		return pbz;
	}

	/**
	 * 返回考勤记录
	 * 以排班日期作为一天考勤
	 * @return
	 */
	public static List<KaoqinRecord> getKaoqinxx(String id, String date) {
		List<KaoqinRecord> kaos = new ArrayList<KaoqinRecord>();
		String sql = "select * from uf_kqxx where kqry=? and pbrq=?";

		RecordSet rs = new RecordSet();
		rs.executeQuery(sql, id, date);
		while (rs.next()) {
			KaoqinRecord kao = new KaoqinRecord();
			kao.setBcbh(rs.getString("bcbh"));
			kao.setDkcard(rs.getString("dkcard"));
			kao.setDkcardstatus(rs.getString("dkcardstatus"));
			kao.setEid(rs.getString("eid"));
			kao.setFlowdataid(rs.getString("flowdataid"));
			kao.setFormmodeid(rs.getString("formmodeid"));
			kao.setId(rs.getString("id"));
			kao.setIsflow(rs.getString("isflow"));
			kao.setKqdbh(rs.getString("kqdbh"));
			kao.setKqdd(rs.getString("kqdd"));
			kao.setKqdjwd(rs.getString("kqdjwd"));
			kao.setKqdkjd(rs.getString("kqdkjd"));
			kao.setKqdkwd(rs.getString("kqdkwd"));
			kao.setKqlx(rs.getString("kqlx"));
			kao.setKqrdkdd(rs.getString("kqrdkdd"));
			kao.setKqrq(rs.getString("kqrq"));
			kao.setKqry(rs.getString("kqry"));
			kao.setKqsj(rs.getString("kqsj"));
			kao.setModedatacreatedate(rs.getString("modedatacreatedate"));
			kao.setModedatacreater(rs.getString("modedatacreater"));
			kao.setModedatacreatertype(rs.getString("modedatacreatertype"));
			kao.setModedatacreatetime(rs.getString("modedatacreatetime"));
			kao.setPbrq(rs.getString("pbrq"));
			kao.setPbsj(rs.getString("pbsj"));
			kao.setRemarks(rs.getString("remarks"));
			kao.setRequestId(rs.getString("requestId"));
			kao.setSbxb(rs.getString("sbxb"));
			kaos.add(kao);
		}
		return kaos;

	}

	// 获取当日打卡信息
	public static Map<String, Object> getdkxx(String id, String date) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("card1", "");
		map.put("card2", "");
		map.put("card3", "");
		map.put("card4", "");
		map.put("card5", "");
		map.put("card6", "");
		map.put("card7", "");
		map.put("card8", "");
		String currentdate = DateUtil.getCurrentDateTime("yyyy-MM-dd");
		if (DateUtil.compare_date(date, currentdate, "yyyy-MM-dd") == 1) {// 如果查询日期大于系统当前日期，则查询的是未来考勤,打卡情况为空
			return map;
		}
		List<KaoqinRecord> kaoqinrecords = getKaoqinxx(id, date);
		Pbzxx pbz = getPbzxx(id, date);
//		// 流程归档日期必须在申请的内容开始日期之前或者=开启日期
//		String sqlstr = "select top 1* from uf_pbbdxx where  applicant =? and convert(date,changeDate,20) =? "
//				+ "and convert(date,confirmdate,20)<=convert(date,datestart,20) order by id desc ";
		String sqlstr = "select top 1* from uf_pbbdxx where  applicant =? and convert(date,changeDate,20) =? "
				+ " order by id desc ";
		RecordSet rste = new RecordSet();
		boolean flag = rste.executeQuery(sqlstr, id, date);
		if (flag && rste.next()) {// 如果查询到当天有流程数据，则用流程的排班覆盖
			String type = rste.getString("type");
			String applicant = rste.getString("applicant");
			String card1 = rste.getString("card1");
			String card2 = rste.getString("card2");
			String card3 = rste.getString("card3");
			String card4 = rste.getString("card4");
			String cardtimes = rste.getString("cardtimes");
			String changeDate = rste.getString("changeDate");
			String changepbday = rste.getString("changepbday");
			String confirmdate = rste.getString("confirmdate");
			String dateend = rste.getString("dateend");
			String datestart = rste.getString("datestart");
			String endT1 = rste.getString("endT1");
			String endT2 = rste.getString("endT2");
			String endT3 = rste.getString("endT3");
			String endT4 = rste.getString("endT4");
			String endTime = rste.getString("endTime");
			String remarks = rste.getString("remarks");
			String requestId = rste.getString("requestId");
			String scan1 = rste.getString("scan1");
			String scan2 = rste.getString("scan2");
			String scan3 = rste.getString("scan3");
			String scan4 = rste.getString("scan4");
			String shenqindate = rste.getString("shenqindate");
			String shift = rste.getString("shift");
			String startTime = rste.getString("startTime");
			String title = rste.getString("title");
			String xhours = rste.getString("xhours");
			String xtype = rste.getString("xtype");
			if (!"6".equals(type)) {// 非平日加班流程(打卡班次覆盖)
				pbz = new Pbzxx(shift, title, scan1, card1, endT1, scan2, card2, endT2, scan3, card3, endT3, scan4,
						card4, endT4, xhours, cardtimes);
				if ("0".equals(type)) {// 请假
					pbz.setTitle("请假");// 改变显示名称为请假
					pbz.setShift("OA_QJ");// 设置类型为请假
				}else if("4".equals(type)) {
					pbz.setTitle("出差");// 改变显示名称为出差
					pbz.setShift("OA_CC");// 设置类型为出差
				}else if("7".equals(type)) {
					pbz.setTitle("出差加班");// 改变显示名称为出差加班
					pbz.setShift("OA_CCJB");// 设置类型为出差加班
				}
			} else {// 平日加班追加打卡班次
				pbz.setCard5(card1);
				pbz.setCard6(card2);
				pbz.setCard7(card3);
				pbz.setCard8(card4);
				pbz.setScan5(scan1);
				pbz.setScan6(scan2);
				pbz.setScan7(scan3);
				pbz.setScan8(scan4);
				pbz.setEndT5(endT1);
				pbz.setEndT6(endT2);
				pbz.setEndT7(endT3);
				pbz.setEndT8(endT4);
				try {
					int cardTimes = Integer.parseInt(pbz.getCardTimes()) + Integer.parseInt(cardtimes);
					pbz.setCardTimes(String.valueOf(cardTimes));
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			}
		}

		if (pbz != null&&!"OA_CC".equals(pbz.getShift())&&!"OA_CCJB".equals(pbz.getShift())) {//出差不用打卡,默认不用显示打卡
			if(isNotEmptyNULl(pbz.getCard1())) {
				map.put("card1", "上班 <span style='color:#ff5722'>缺卡</span>");
			}
			if(isNotEmptyNULl(pbz.getCard2())) {
				map.put("card2", "下班 <span style='color:#ff5722'>缺卡</span>");
			}
			if(isNotEmptyNULl(pbz.getCard3())) {
				map.put("card3", "上班 <span style='color:#ff5722'>缺卡</span>");
			}
			if(isNotEmptyNULl(pbz.getCard4())) {
				map.put("card4", "下班 <span style='color:#ff5722'>缺卡</span>");
			}
			if(isNotEmptyNULl(pbz.getCard5())) {
				map.put("card5", "上班 <span style='color:#ff5722'>缺卡</span>");
			}
			if(isNotEmptyNULl(pbz.getCard6())) {
				map.put("card6", "下班 <span style='color:#ff5722'>缺卡</span>");
			}
			if(isNotEmptyNULl(pbz.getCard7())) {
				map.put("card7", "上班 <span style='color:#ff5722'>缺卡</span>");
			}
			if(isNotEmptyNULl(pbz.getCard8())) {
				map.put("card8", "下班 <span style='color:#ff5722'>缺卡</span>");
			}
			
			for (KaoqinRecord kaoqinRecord : kaoqinrecords) {
				if (isNotEmptyNULl(pbz.getCard1())&&"card1".equals(kaoqinRecord.getDkcard())) {//如果需要打卡，且有打卡记录则判断是否异常
					if("0".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card1", "上班 " + kaoqinRecord.getKqsj() + " ");
					}else if("1".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card1", "上班 " + kaoqinRecord.getKqsj() + " <span style='color:#ff5722'>迟到</span>");
					}
				}
				if (isNotEmptyNULl(pbz.getCard2())&&"card2".equals(kaoqinRecord.getDkcard())) {
					if("0".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card2", "下班 " + kaoqinRecord.getKqsj() + " ");
					}else if("2".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card2", "下班 " + kaoqinRecord.getKqsj() + " <span style='color:#ff5722'>早退</span>");
					}
				}
				if (isNotEmptyNULl(pbz.getCard3())&&"card3".equals(kaoqinRecord.getDkcard())) {
					if("0".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card3", "上班 " + kaoqinRecord.getKqsj() + " ");
					}else if("1".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card3", "上班 " + kaoqinRecord.getKqsj() + " <span style='color:#ff5722'>迟到</span>");
					}
				}
				if (isNotEmptyNULl(pbz.getCard4())&&"card4".equals(kaoqinRecord.getDkcard())) {
					if("0".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card4", "下班 " + kaoqinRecord.getKqsj() + " ");
					}else if("2".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card4", "下班 " + kaoqinRecord.getKqsj() + " <span style='color:#ff5722'>早退</span>");
					}
				}
				if (isNotEmptyNULl(pbz.getCard5())&&"card5".equals(kaoqinRecord.getDkcard())) {//如果需要打卡，且有打卡记录则判断是否异常
					if("0".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card5", "上班 " + kaoqinRecord.getKqsj() + " ");
					}else if("1".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card5", "上班 " + kaoqinRecord.getKqsj() + " <span style='color:#ff5722'>迟到</span>");
					}
				}
				if (isNotEmptyNULl(pbz.getCard6())&&"card6".equals(kaoqinRecord.getDkcard())) {
					if("0".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card6", "下班 " + kaoqinRecord.getKqsj() + " ");
					}else if("2".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card6", "下班 " + kaoqinRecord.getKqsj() + " <span style='color:#ff5722'>早退</span>");
					}
				}
				if (isNotEmptyNULl(pbz.getCard7())&&"card7".equals(kaoqinRecord.getDkcard())) {
					if("0".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card7", "上班 " + kaoqinRecord.getKqsj() + " ");
					}else if("1".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card7", "上班 " + kaoqinRecord.getKqsj() + " <span style='color:#ff5722'>迟到</span>");
					}
				}
				if (isNotEmptyNULl(pbz.getCard8())&&"card8".equals(kaoqinRecord.getDkcard())) {
					if("0".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card8", "下班 " + kaoqinRecord.getKqsj() + " ");
					}else if("2".equals(kaoqinRecord.getDkcardstatus())) {
						map.put("card8", "下班 " + kaoqinRecord.getKqsj() + " <span style='color:#ff5722'>早退</span>");
					}
				}
			}
			
		}
		return map;
	}

	public static Map<String, Object> getinfo(String id, String date) {
		Map<String, Object> mapinfo = new HashMap<String, Object>();
		Map<String, Integer> calendarWork = getcalendarDays(date);
		mapinfo.put("calendarWork", calendarWork);
		Map<String, Integer> planWork = getPaiBanDays(id, date);
		mapinfo.put("planWork", planWork);
		Map<String, Object> currentDay = getdkxx(id, date);// 当天考勤情况
		mapinfo.put("currentDay", currentDay);
		return mapinfo;
	}

	/**
	 * 查询人员截至到当天的当月的排班 type -1为将来日期，0 休息，1正常(包含迟到、早退、外勤)，2缺卡异常，3请假
	 */
	public static List<Map<String, Object>> getMonthPb(String id, String date) {

		date = StringUtils.trim(date).substring(0, 7);// 去掉日期，保留年月
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		RecordSet rs = new RecordSet();
		String sql = "select h.id as hid,ry.eid,ry.name, convert(VARCHAR(10),ry.term,20) as term ,ry.xType,pbz.* from hrmResource h "
				+ "inner join uf_rypbxx ry on h.id=? and convert(VARCHAR(7),ry.term,20)=?  and h.outkey=ry.eid "
				+ "inner join uf_pbzxx pbz on (pbz.IsDisabled='0' or pbz.IsDisabled is null) and ry.shift=pbz.shift order by convert(date,ry.term,20)";

		rs.executeQuery(sql, id, date);
		while (rs.next()) {
			Map<String, Object> map = new HashMap<String, Object>();
			String pdate = rs.getString("term");
//			// 流程归档日期必须在申请的内容开始日期之前或者=开启日期
//			String sqlstr = "select top 1* from uf_pbbdxx where  applicant =? and convert(date,changeDate,20) =? "
//					+ "and convert(date,confirmdate,20)<=convert(date,datestart,20) order by id desc ";
			String sqlstr = "select top 1* from uf_pbbdxx where  applicant =? and convert(date,changeDate,20) =? "
					+ " order by id desc ";
			RecordSet rste = new RecordSet();
			boolean flag = rste.executeQuery(sqlstr, id, pdate);

			Pbzxx pbz = new Pbzxx();// 当日排班信息
			pbz.setShift(rs.getString("shift"));
			pbz.setTitle(rs.getString("title"));
			pbz.setCard1(rs.getString("card1"));
			pbz.setCard2(rs.getString("card2"));
			pbz.setCard3(rs.getString("card3"));
			pbz.setCard4(rs.getString("card4"));
			pbz.setScan1(rs.getString("scan1"));
			pbz.setScan2(rs.getString("scan2"));
			pbz.setScan3(rs.getString("scan3"));
			pbz.setScan4(rs.getString("scan4"));
			pbz.setEndT1(rs.getString("endT1"));
			pbz.setEndT2(rs.getString("endT2"));
			pbz.setEndT3(rs.getString("endT3"));
			pbz.setEndT4(rs.getString("endT4"));
			pbz.setXhours(rs.getString("Xhours"));
			pbz.setCardTimes(rs.getString("CardTimes"));

			if (flag && rste.next()) {// 如果查询到当天有流程数据，则用流程的排班覆盖
				String type = rste.getString("type");
				String applicant = rste.getString("applicant");
				String card1 = rste.getString("card1");
				String card2 = rste.getString("card2");
				String card3 = rste.getString("card3");
				String card4 = rste.getString("card4");
				String cardtimes = rste.getString("cardtimes");
				String changeDate = rste.getString("changeDate");
				String changepbday = rste.getString("changepbday");
				String confirmdate = rste.getString("confirmdate");
				String dateend = rste.getString("dateend");
				String datestart = rste.getString("datestart");
				String endT1 = rste.getString("endT1");
				String endT2 = rste.getString("endT2");
				String endT3 = rste.getString("endT3");
				String endT4 = rste.getString("endT4");
				String endTime = rste.getString("endTime");
				String remarks = rste.getString("remarks");
				String requestId = rste.getString("requestId");
				String scan1 = rste.getString("scan1");
				String scan2 = rste.getString("scan2");
				String scan3 = rste.getString("scan3");
				String scan4 = rste.getString("scan4");
				String shenqindate = rste.getString("shenqindate");
				String shift = rste.getString("shift");
				String startTime = rste.getString("startTime");
				String title = rste.getString("title");
				String xhours = rste.getString("xhours");
				String xtype = rste.getString("xtype");
				if (!"6".equals(type)) {// 非平日加班流程(打卡班次覆盖)
					pbz = new Pbzxx(shift, title, scan1, card1, endT1, scan2, card2, endT2, scan3, card3, endT3, scan4,
							card4, endT4, xhours, cardtimes);
					if ("0".equals(type)) {// 请假
						pbz.setTitle("请假");// 改变显示名称为请假
						pbz.setShift("OA_QJ");// 设置类型为请假
					}else if("4".equals(type)) {
						pbz.setTitle("出差");// 改变显示名称为出差
						pbz.setShift("OA_CC");// 设置类型为出差
					}else if("7".equals(type)) {//出差加班
						pbz.setTitle("出差加班");// 改变显示名称为出差
						pbz.setShift("OA_CCJB");// 设置类型为出差
					}
				} else {// 平日加班追加打卡班次
					pbz.setCard5(card1);
					pbz.setCard6(card2);
					pbz.setCard7(card3);
					pbz.setCard8(card4);
					pbz.setScan5(scan1);
					pbz.setScan6(scan2);
					pbz.setScan7(scan3);
					pbz.setScan8(scan4);
					pbz.setEndT5(endT1);
					pbz.setEndT6(endT2);
					pbz.setEndT7(endT3);
					pbz.setEndT8(endT4);
					try {
						int cardTimes = Integer.parseInt(pbz.getCardTimes()) + Integer.parseInt(cardtimes);
						pbz.setCardTimes(String.valueOf(cardTimes));
					} catch (NumberFormatException e) {
						e.printStackTrace();
					}
				}
			}

			map.put("date", pdate);
			map.put("title", pbz.getTitle());
			if (DateUtil.compare_date(pdate, DateUtil.getCurrentDateTime("yyyy-MM-dd"), "yyyy-MM-dd") == 1) {// 如果查询日期大于系统当前日期，则查询的是未来考勤,打卡情况-1,不确定
				map.put("type", "-1");
				maps.add(map);
				continue; // 跳过以下判断
			}
			if ("R".equalsIgnoreCase(StringUtils.trim(pbz.getShift()))) {
				map.put("type", "0");// 休息
			} else if ("OA_QJ".equalsIgnoreCase(StringUtils.trim(pbz.getShift()))) {// 请假
				map.put("type", "3");// 请假
			} else if ("OA_CC".equalsIgnoreCase(StringUtils.trim(pbz.getShift()))) {// 出差
				map.put("type", "1");// 出差
			} else if ("OA_CCJB".equalsIgnoreCase(StringUtils.trim(pbz.getShift()))) {// 出差加班
				map.put("type", "1");// 出差加班
			} else {
				map.put("type", "2");// 初始化为异常
				List<KaoqinRecord> kaoqinrecords = getKaoqinxx(id, pdate);
				boolean flag1 = false;
				boolean flag2 = false;
				boolean flag3 = false;
				boolean flag4 = false;
				for (KaoqinRecord kaoqinRecord : kaoqinrecords) {
					if (!isNotEmptyNULl(pbz.getCard1())||"card1".equals(kaoqinRecord.getDkcard())) {//如果card1 不需要打卡或者有考勤记录，则视为正常
						flag1=true;
					}
					if (!isNotEmptyNULl(pbz.getCard2())||"card2".equals(kaoqinRecord.getDkcard())) {//如果card2 不需要打卡或者有考勤记录，则视为正常
						flag2=true;
					}
					if (!isNotEmptyNULl(pbz.getCard3())||"card3".equals(kaoqinRecord.getDkcard())) {//如果card3 不需要打卡或者有考勤记录，则视为正常
						flag3=true;
					}
					if (!isNotEmptyNULl(pbz.getCard4())||"card4".equals(kaoqinRecord.getDkcard())) {//如果card4 不需要打卡或者有考勤记录，则视为正常
						flag4=true;
					}
				}
				if (flag1 && flag2 && flag3 && flag4) {// 4次打卡全部正常
					map.put("type", "1");
				}
			}
			maps.add(map);
		}

		logger.info(JSON.toJSONString(maps));
		return maps;
	}

	/// 判断打卡card班次是否有效
	private static boolean isNotEmptyNULl(String card) {
		if (!StringUtils.isEmpty(StringUtils.trim(card)) && !"NULL".equalsIgnoreCase(StringUtils.trim(card))) {
			return true;
		}
		return false;
	}
}
